package com.imchen.cms.cmscore.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imchen.cms.constant.CMS;
import com.imchen.cms.cmscore.dao.InvestmentMapper;
import com.imchen.cms.cmscore.model.Investment;
import com.imchen.cms.utils.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author imchen
 * @create: 2019-01-15 13:59
 **/
@Service
public class InvestmentService implements BaseService<Investment> {

    Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private InvestmentMapper mapper;

    @Override
    public HashMap<String, Integer> add(Investment investment) {
        int count= mapper.insertSelective(investment);

        return ServiceUtil.toAddMap(count,investment.getId());
    }

    @Override
    public Investment  findById(int id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int removeById(int id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Investment investment) {
        return mapper.updateByPrimaryKeySelective(investment);
    }

    public List<Investment> findByPage(int currentIndex, int pageSize){
        return mapper.selectInvestments(currentIndex,pageSize);
    }

    public int deleteById(int id){
        return mapper.deleteByPrimaryKeyLogic(id, CMS.DATA_IS_DELETE);
    }

    public int restoreById(int id){
        return mapper.deleteByPrimaryKeyLogic(id, CMS.DATA_NOT_DELETE);
    }

    public PageInfo<Investment> findByPageHelper(int index, int pageSize){
        PageHelper.startPage(index,pageSize);
        List<Investment> investmentList=mapper.selectAllInvestments();
        return new PageInfo<>(investmentList);
    }

    public List<Investment> findByDate(String startTime,String endTime) throws ParseException {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);
        return mapper.selectByBetweenDate(startDate,endDate);
    }
}